//
//  PCEditCommentController.m
//  PhotoCircle
//
//  Created by GL on 15/12/29.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCEditCommentController.h"
#import "GMPhotoView.h"
#import "PCUserModel.h"
#import "GMTipsHelper.h"
#import "fetchData.h"
#import "PCCommentModel.h"
#import "photoBtnDelegate.h"
#import "photoBtnView.h"

@interface PCEditCommentController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,GMPhotoViewDelegate,UIActionSheetDelegate,UIAlertViewDelegate,photoBtnDelegate>

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIButton *addImgButton;
@property (nonatomic, strong) UIImage *cImage;
@property (nonatomic, strong) GMPhotoView *photoView;
@property (nonatomic, strong) PCUserModel *userModel;
@property (nonatomic, strong) photoBtnView *photoButtonView;

@end

@implementation PCEditCommentController

-(PCUserModel *)userModel{
    _userModel = [[PCUserInfoManager sharedUserInfo] readUserInfo];
    return _userModel;
}

-(void)configUI{
    
    _textView.layer.cornerRadius = 10;
    _textView.layer.masksToBounds = YES;
    //设置边框及边框颜色
    _textView.layer.borderWidth = 0.5;
    _textView.layer.borderColor =[PCColorC5 CGColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configUI];
    // Do any additional setup after loading the view.
}

- (IBAction)backEvent:(UIButton *)sender {
    [GMTipsHelper showDismiss];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)sendCommentEvent:(UIButton *)sender {
    
    if (self.textView.text.length ==0) {
        [GMTipsHelper showFail:@"评论不能为空"];
        return;
    }
    
    [GMTipsHelper showLoading];
    NSDictionary *pDic = nil;
    pDic = @{@"content":self.textView.text,@"token":self.userModel.userToken,@"item_uuid":self.albumId,@"item_type":self.item_type};
    NSData *imgData = UIImageJPEGRepresentation(self.cImage, 0.8);
    [[fetchData shared] postSubmitCommentDataWithPatameterDic:pDic imageData:imgData sucessBlock:^(PCCommentModel *myCommemtModel) {
        [GMTipsHelper showSuccess:@"评论成功"];
    } otherCodeBlock:^(NSInteger code) {
        if (code == API_RESPONSE_OK) {
            [GMTipsHelper showSuccess:@"评论成功"];
            if ([self.delegate respondsToSelector:@selector(submitCommentSussecc)]) {
                [self.delegate submitCommentSussecc];
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:PHOTOCOMMENTCOUNT_NOTIFICATION_NAME object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshCommentList" object:nil];
            [self performSelector:@selector(popViewController) withObject:nil afterDelay:0.8];
        }else if (code == API_RESPONSE_INVALIDTOKEN){
            [GMTipsHelper showFail:@"登录失效"];
        }else{
            [GMTipsHelper showFail:@"评论失败"];
        }
    } failWithBlock:^{
        [GMTipsHelper showBadNetwork];
    }];
    
}

-(void)popViewController
{
    [self backEvent:nil];
}

#pragma mark -
#pragma mark - Button Event
- (IBAction)addImageEvent:(UIButton *)sender {
    
    [self.textView resignFirstResponder];

    if (self.cImage) {
        
        _photoView = [[GMPhotoView alloc] initWithFrame:self.navigationController.view.bounds andImage:self.cImage rightItemType:GMPhotoRightItemType_Delete];
        _photoView.pDelegate = self;
        _photoView.alpha = 1.0;
        [self.navigationController.view addSubview:_photoView];
        
    }else {
        [self selectedImage];
    }
}

// 选择图片
-(void)selectedImage
{
    UINib *nib = [UINib nibWithNibName:@"photoBtnView" bundle:[NSBundle mainBundle]];
    _photoButtonView = [[nib instantiateWithOwner:nil options:nil] firstObject];
    _photoButtonView.photoBtnDelegate = self;
    [self.navigationController.view addSubview:_photoButtonView];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        //相册上传
        UIImagePickerController *imagePickerVC=[[UIImagePickerController alloc] init];
        imagePickerVC.delegate=self;
        imagePickerVC.allowsEditing = YES;
        imagePickerVC.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePickerVC animated:YES completion:nil];
        
    }else if (buttonIndex == 1){
        // 拍照
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

#pragma mark -
#pragma mark -Photo Delegate

-(void)tipDeletedImage
{
    if (iOS(8.0)) {
        
        UIAlertController * cancleAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否删除图片" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [_photoView hidenPhotoView];
            
            [self.addImgButton setImage:[UIImage imageNamed:@"PCAddImageItem"] forState:UIControlStateNormal];
            [self.addImgButton setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
            
            self.cImage = nil;
        }];
        
        UIAlertAction *cancleAcion = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [cancleAlert addAction:okAciton];
        [cancleAlert addAction:cancleAcion];
        [self presentViewController:cancleAlert animated:YES completion:^{
        }];
    }else{
        
        UIAlertView *cancleAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否删除图片" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        //cancleAlert.tag =CancleTag;
        cancleAlert.delegate = self;
        [cancleAlert show];
    }
}

-(void)photographUpload
{
    _photoButtonView.hidden = YES;
    [_photoButtonView removeFromSuperview];
    
    // 拍照
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    [self presentViewController:imagePicker animated:YES completion:nil];
    
}

-(void)albumUpload
{
    _photoButtonView.hidden = YES;
    [_photoButtonView removeFromSuperview];
    
    //相册上传
    UIImagePickerController *imagePickerVC=[[UIImagePickerController alloc] init];
    imagePickerVC.delegate=self;
    imagePickerVC.allowsEditing = YES;
    imagePickerVC.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}

-(void)cancelAction
{
    _photoButtonView.hidden = YES;
    [_photoButtonView removeFromSuperview];
}

#pragma mark -
#pragma mark -PCLogoutCellDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        //[self logoutSheying];
        [_photoView hidenPhotoView];
        
        [self.addImgButton setImage:[UIImage imageNamed:@"PCAddImageItem"] forState:UIControlStateNormal];
        self.cImage = nil;

    }else{
        
    }
}

#pragma mark - image picker delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    self.cImage = [UIImage imageWithData:imgData];
    // 添加图片
    [self.addImgButton setImage:image forState:UIControlStateNormal];
    [self.addImgButton setImage:image forState:UIControlStateHighlighted];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
